package com.xhu.controller;

import com.xhu.base.MetaDataManager;
import org.springframework.stereotype.Controller;
import org.springframework.stereotype.Repository;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import java.util.List;

import static org.apache.log4j.spi.Configurator.NULL;

/**
 * Created by yuan on 2019/3/29.
 */
@Controller
@RequestMapping("/create") // url和类绑定
public class CreateController {
    @RequestMapping("/createTable")
    public String CreateTable(HttpServletRequest httpServletRequest,ModelMap modelMap) {
        String tableName = httpServletRequest.getParameter("tableName");
        //判断表名是否存在
        MetaDataManager manager = new MetaDataManager();
        List tableList = manager.getAllTableName();
        boolean bool = true;
        for (int i = 0; i < tableList.size(); i++) {
            if (tableList.get(i).equals(tableName)) {
                bool = false;
            }
        }
        if (!bool) {
            modelMap.addAttribute("modelAdd", 0);
            return "forward:../newTable.jsp";
        } else {
            StringBuffer SQL = new StringBuffer();
            String str = httpServletRequest.getParameter("num");
            int i = Integer.parseInt(str);
            for (int j = 1; j <= i; j++) {
                String columnName = httpServletRequest.getParameter("columnName" + j);
                String typess = httpServletRequest.getParameter("typess" + j);
                String lengthss = httpServletRequest.getParameter("lengthss" + j);
                SQL = SQL.append(columnName + " " + typess + "(" + lengthss + "),");
            }
            SQL.deleteCharAt(SQL.length() - 1);
            String sql = "create table " + tableName + "(" + SQL + ")";
            //调用客户端接口
            Client.ClientDo(sql);
            modelMap.addAttribute("modelAdd", 1);
            System.out.println(sql);
            return "forward:../newTable.jsp";
        }
    }
}
